<script setup>
import { ref, onMounted, onUnmounted, nextTick } from 'vue'
import * as echarts from 'echarts'
import CountUp from 'vue-countup-v3'

const chartRef = ref(null)
let chart = null

const timeRanges = [
  { label: '近6月', value: '6m' },
  { label: '近1年', value: '1y' }
]

const currentRange = ref('6m')

const initChart = () => {
  if (!chartRef.value) return
  
  chart = echarts.init(chartRef.value)
  const option = {
    grid: {
      top: '10%',
      left: '3%',
      right: '4%',
      bottom: '8%',
      containLabel: true
    },
    xAxis: {
      type: 'category',
      data: ['1月', '2月', '3月', '4月', '5月', '6月'],
      axisLine: {
        lineStyle: { color: 'rgba(255, 255, 255, 0.1)' }
      },
      axisLabel: {
        color: '#94A3B8',
        fontSize: 12
      }
    },
    yAxis: {
      type: 'value',
      min: 60,
      max: 100,
      splitNumber: 4,
      axisLine: { show: false },
      axisTick: { show: false },
      splitLine: {
        lineStyle: { color: 'rgba(255, 255, 255, 0.05)' }
      },
      axisLabel: {
        color: '#94A3B8',
        fontSize: 12,
        formatter: '{value}%'
      }
    },
    series: [
      {
        data: [62, 65, 70, 75, 80, 85],
        type: 'line',
        smooth: true,
        symbol: 'circle',
        symbolSize: 8,
        lineStyle: {
          width: 3,
          color: '#60A5FA'
        },
        itemStyle: {
          color: '#60A5FA',
          borderWidth: 2,
          borderColor: '#1E293B'
        },
        areaStyle: {
          color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
            { offset: 0, color: 'rgba(96, 165, 250, 0.2)' },
            { offset: 1, color: 'rgba(96, 165, 250, 0)' }
          ])
        }
      }
    ],
    tooltip: {
      trigger: 'axis',
      backgroundColor: 'rgba(0, 0, 0, 0.8)',
      borderColor: 'rgba(255, 255, 255, 0.1)',
      borderWidth: 1,
      padding: [8, 12],
      textStyle: { color: '#fff' },
      formatter: '{b}: {c}%'
    }
  }
  
  chart.setOption(option)
}

const handleResize = () => {
  if (chart) {
    chart.resize()
  }
}

onMounted(async () => {
  await nextTick()
  initChart()
  window.addEventListener('resize', handleResize)
})

onUnmounted(() => {
  if (chart) {
    chart.dispose()
    chart = null
  }
  window.removeEventListener('resize', handleResize)
})
</script>
<template>
  <div class="chart-card">
    <div class="chart-header">
      信用修复率趋势
      <div class="time-range">
        <button 
          v-for="range in timeRanges" 
          :key="range.value"
          :class="['range-btn', { active: currentRange === range.value }]"
          @click="currentRange = range.value"
        >
          {{ range.label }}
        </button>
      </div>
    </div>
    <div class="chart-content">
      <div class="metrics">
        <div class="metric-item">
          <div class="metric-label">当前修复率</div>
          <div class="metric-value">
            <CountUp :end-val="85.6" :decimals="1" :duration="2" />
            <span class="unit">%</span>
          </div>
          <div class="metric-trend positive">
            <i class="fas fa-arrow-up"></i>
            <span>5.2%</span>
          </div>
        </div>
        <div class="metric-item">
          <div class="metric-label">平均修复时长</div>
          <div class="metric-value">
            <CountUp :end-val="15.3" :decimals="1" :duration="2" />
            <span class="unit">天</span>
          </div>
          <div class="metric-trend negative">
            <i class="fas fa-arrow-down"></i>
            <span>2.1天</span>
          </div>
        </div>
      </div>
      <div class="chart-container" ref="chartRef">
        <div class="chart-wrapper"></div>
      </div>
    </div>
  </div>
</template>
<style scoped lang="scss">
.chart-card {
  background: rgba(30, 41, 59, 0.5);
  border: 1px solid rgba(255, 255, 255, 0.1);
  border-radius: 8px;
  padding: 20px;
  backdrop-filter: blur(8px);
  height: 100%;
}

.chart-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 24px;
  color: #94A3B8;
  font-size: 16px;
  font-weight: 500;
}

.time-range {
  display: flex;
  gap: 1px;
  padding: 3px;
  background: rgba(255, 255, 255, 0.1);
  border-radius: 6px;
}

.range-btn {
  padding: 6px 16px;
  font-size: 12px;
  color: #94A3B8;
  border-radius: 4px;
  transition: all 0.3s ease;
  
  &:hover:not(.active) {
    color: #E2E8F0;
    background: rgba(255, 255, 255, 0.05);
  }
  
  &.active {
    background: rgba(96, 165, 250, 0.2);
    color: #60A5FA;
    font-weight: 500;
  }
}

.chart-content {
  height: calc(100% - 56px);
  display: flex;
  flex-direction: column;
  gap: 16px;
  padding-top: 4px;
}

.metrics {
  display: flex;
  gap: 24px;
  flex-shrink: 0;
}

.metric-item {
  flex: 1;
  background: rgba(255, 255, 255, 0.03);
  border-radius: 8px;
  padding: 12px;
}

.metric-label {
  color: #94A3B8;
  font-size: 13px;
  margin-bottom: 4px;
}

.metric-value {
  color: #E2E8F0;
  font-size: 22px;
  font-weight: 600;
  font-family: 'JetBrains Mono', monospace;
  display: flex;
  align-items: baseline;
  gap: 4px;
  margin-bottom: 4px;
  
  .unit {
    color: #94A3B8;
    font-size: 12px;
    font-weight: normal;
  }
}

.metric-trend {
  display: flex;
  align-items: center;
  gap: 4px;
  font-size: 13px;
  
  &.positive {
    color: #34D399;
  }
  
  &.negative {
    color: #F87171;
  }
  
  i {
    font-size: 10px;
  }
}

.chart-container {
  width: 100%;
  flex: 1;
  min-height: 0;
  position: relative;
  display: flex;
  flex-direction: column;
  
  .chart-wrapper {
    flex: 1;
    min-height: 0;
    width: 100%;
    height: 100%;
  }
}
</style>
